package cn.frh.org.service.impl;

import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.basic.util.MD5Utils;
import cn.frh.basic.util.StrUtils;
import cn.frh.org.domain.Employee;
import cn.frh.org.mapper.EmployeeMapper;
import cn.frh.org.service.IEmployeeService;
import cn.frh.user.domain.Logininfo;
import cn.frh.user.mapper.LogininfoMapper;
import com.sun.org.apache.xml.internal.security.Init;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;



    @Override
    public void add(Employee employee) {
        //初始化参数
        initEmployee(employee);
        Logininfo logininfo = Employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        employee.setLogininfo_id(logininfo.getId());
        employeeMapper.save(employee);
    }

    private void initEmployee(Employee employee) {
        //设置盐值
        String salt = StrUtils.getComplexRandomString(32);
        //加密加盐
        String md5pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        //设置盐值
        employee.setSalt(salt);
        //设置密码
        employee.setPassword(md5pwd);
    }

    private Logininfo Employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        //后台员工为0
        logininfo.setType(0);
        return logininfo;
    }


}
